//
//  WQStatusViewModel.swift
//  sanwuWeibo
//
//  Created by 王先强 on 15/12/10.
//  Copyright © 2015年 王先强. All rights reserved.
//

import UIKit

class WQStatusViewModel {
    //微博模型
    var status: WQStatus?
    //每个微博的图片固定，存储性属性
    var thumbnailUrls = [NSURL]()
    //存储每个tableViewCell的行高
    lazy var rowHeight: CGFloat = {
        
        var tempCell: WQHomeCell?
        if self.status!.retweeted_status != nil {
            tempCell = WQStatusRetweetedCell(style: .Default, reuseIdentifier: homeStatusRetweetedCellID)
        }else{
            tempCell = WQStatusNormalCell(style: .Default, reuseIdentifier: homeStatusNormalCellID)
        }

        //利用WQStatusViewModel,调用cell的rowHeight方法计算出cell的高度
        return tempCell!.rowHeight(self)
    }()
    
    
    init(status: WQStatus){
        self.status = status
        //给ViewModel的thumbnailUrls赋值
        //1.原创微博有图片，转发微博就没有图片，
        //2.转发微博有图片，原创微博就不会有图片，只需要用一个url图片数组
        guard let dict = status.pic_urls ?? status.retweeted_status?.pic_urls else {
            return
        }

        for urlDict in dict {
            let url = NSURL(string: urlDict["thumbnail_pic"]!)
            thumbnailUrls.append(url!)
        }
    }
    
    //微博的cellID
    var cellID: String {
        return status?.retweeted_status == nil ? homeStatusNormalCellID : homeStatusRetweetedCellID
    }
    

    //转发微博文本
    var retweetedStatusContent : String? {
        guard let retweetedStatus = self.status!.retweeted_status else {
            return nil
        }
        
        return "@" + "\(retweetedStatus.user?.screen_name ?? "")" + ":" + (retweetedStatus.text ?? "")
    }
    //微博用户头像url
    var statusUserIconUrl: NSURL {
        
        return NSURL(string: status?.user?.profile_image_url ?? "")!
    }
    var statusUserDefaultIcon: UIImage {
        return UIImage(named: "avatar_default_big")!
    }
    var statusUserName: String {
        return (status?.user?.screen_name)!
    }
    var statusUserMbrank: UIImage? {
        
        
        let level = (status?.user?.mbrank)!
        return (( level < 7 && level > 0) ? UIImage(named: "common_icon_membership_level\(level)") :  nil)
    }
    var statusCreatedat: String {
        return (status?.created_at)!
    }
    var statusSource: String {
        return (status?.source)!
    }
    var statusContent: String {
        return (status?.text)!
    }
    /// 认证类型，-1：没有认证，0，认证用户，2,3,5: 企业认证，220: 达人
    var verifiedType: UIImage? {
        
        switch (status?.user?.verified_type ?? -1) {
        case 0: return UIImage(named: "avatar_vip")
        case 2, 3, 5: return UIImage(named: "avatar_enterprise_vip")
        case 220: return UIImage(named: "avatar_grassroot")
        default: return nil
        }
    }
   
    
    
}
